import wx

class Renderer(object):

    def __init__(self):
        '''
         Renderer
        '''
    def renderExtent(self, color, mdc, map_transform, extent):
        min = map_transform.worldToMap(extent.getMinX(), extent.getMinY())
        max = map_transform.worldToMap(extent.getMaxX(), extent.getMaxY())
        mdc.SetPen(wx.Pen(color, 1))
        mdc.DrawLine(min.x, min.y, min.x, max.y)
        mdc.DrawLine(min.x, min.y, max.x, min.y)
        mdc.DrawLine(max.x, min.y, max.x, max.y)
        mdc.DrawLine(min.x, max.y, max.x, max.y)
    
    def renderBB(self, mdc, bb_extent, map_transform):
        pos_min = map_transform.worldToMap(bb_extent.getMinX(), bb_extent.getMinY())
        pos_max = map_transform.worldToMap(bb_extent.getMaxX(), bb_extent.getMaxY())
        mdc.SetPen(wx.Pen('red', 1))
        mdc.DrawLine(pos_min.x, pos_min.y, pos_min.x, pos_max.y)
        mdc.DrawLine(pos_min.x, pos_min.y, pos_max.x, pos_min.y)
        mdc.DrawLine(pos_max.x, pos_min.y, pos_max.x, pos_max.y)
        mdc.DrawLine(pos_min.x, pos_max.y, pos_max.x, pos_max.y)
    
    def renderBB2(self, mdc, bb_extent, map_transform):
        pos_min = map_transform.worldToMap(bb_extent.getMinX(), bb_extent.getMinY())
        pos_max = map_transform.worldToMap(bb_extent.getMaxX(), bb_extent.getMaxY())
        mdc.SetPen(wx.Pen('green', 1))
        mdc.DrawLine(pos_min.x, pos_min.y, pos_min.x, pos_max.y)
        mdc.DrawLine(pos_min.x, pos_min.y, pos_max.x, pos_min.y)
        mdc.DrawLine(pos_max.x, pos_min.y, pos_max.x, pos_max.y)
        mdc.DrawLine(pos_min.x, pos_max.y, pos_max.x, pos_max.y)
    
    def draw(self, mdc, image, tile, transform):
        tile_info = self.rescaleImage(image, tile, transform)
        mdc.DrawBitmap(tile_info[0], tile_info[1].x,tile_info[2].y)
        self.drawTileExtent(mdc, tile, transform)
    
    def rescaleImage(self, image, tile, transform):
        point1 = transform.worldToMap(tile.extent.getMinX(), tile.extent.getMinY())
        point2 = transform.worldToMap(tile.extent.getMaxX(), tile.extent.getMaxY())

        point1.x = round(point1.x)
        point1.y = round(point1.y)
        point2.x = round(point2.x)
        point2.y = round(point2.y)
        
        rescaled_image = image.Rescale((point2.x - point1.x), (point1.y - point2.y))
        bitmap = wx.BitmapFromImage(rescaled_image)
        
        return (bitmap, point1, point2)
    
    def drawTileExtent(self, mdc, tile, transform):
        
        pos_min = transform.worldToMap(tile.extent.getMinX(), tile.extent.getMinY())
        pos_max = transform.worldToMap(tile.extent.getMaxX(), tile.extent.getMaxY())
        
        mdc.SetPen(wx.Pen('blue', 1))
        mdc.DrawLine(pos_min.x, pos_min.y, pos_min.x, pos_max.y)
        mdc.DrawLine(pos_min.x, pos_min.y, pos_max.x, pos_min.y)
        mdc.DrawLine(pos_max.x, pos_min.y, pos_max.x, pos_max.y)
        mdc.DrawLine(pos_min.x, pos_max.y, pos_max.x, pos_max.y)